package com.huang.annotation_reflection;

import java.lang.annotation.*;

public class reflection10 {

    public static void main(String[] args) throws NoSuchFieldException {
        Class c1 = Student2.class;
        //获得注解
        System.out.println("获得注解");
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获得注解Value值
        System.out.println("获得注解Value值");
        MyTable myTable = (MyTable) c1.getAnnotation(MyTable.class);
        System.out.println(myTable.value());

        //获得类指定的注解和值
        System.out.println("获得类指定的注解和值");
        MyFiled myFiled = c1.getDeclaredField("name").getAnnotation(MyFiled.class);
        System.out.println(myFiled.colName());
        System.out.println(myFiled.type());
        System.out.println(myFiled.length());

    }
}

@MyTable("db_student2")
class Student2 {
    @MyFiled(colName = "db_name",type = "varchar",length = 10)
    String name;
    @MyFiled(colName = "db_id",type = "int",length = 20)
    int id;
    @MyFiled(colName = "db_age",type = "int",length = 3)
    int age;

    public Student2() {
    }

    public Student2(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyTable {
    String value();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyFiled {
    String colName();

    String type();

    int length();
}